app.controller('RtOrderCtrl', function ($scope, ajax, $modal, toaster) {

	var deleter = new Req.DELETE({ resource: $scope.resource });

	$scope.statusList = [
		{ id: 1, name: "待付款" },
		{ id: 2, name: "支付中" },
		{ id: 3, name: "已支付" },
		{ id: 4, name: "已完成" },
		{ id: 5, name: "已取消" },
		{ id: 6, name: "订单超时" },
		{ id: 7, name: "退款中" },
		{ id: 8, name: "退款完成" },
		{ id: 9, name: "退款失败" }
	];

	$scope.reload = $scope.page.loader('order/rt/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.reload(1);

	//模糊查询
	$scope.queryMap = {};
	$scope.timeRange = {};
	$scope.timeDeparture = {};
	$scope.query = function (page) {
		if ($scope.statusList.selected != undefined) {
			$scope.queryMap.status = $scope.statusList.selected.id;
		} else if ($scope.statusList.selected == undefined) {
			delete $scope.queryMap.status;
		}

		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			$scope.queryMap.createTime = starTime + "#@" + endTime;
		} else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
			delete $scope.queryMap.createTime;
		}
		$scope.reload(page || 1, $scope.queryMap);
	};

	//删除
	$scope.del = function (size, current_data, submit_type) {
		var modalInstance = $scope.delModal(size, current_data, submit_type);
		modalInstance.result.then(function (param) {
			if (param.type == 'delete') {
				deleter.id = param.data.id;
				deleter.resource = 'order/rt/' + deleter.id + '/delete';
				ajax.call(deleter).then(function (result) {
					if (result.code == 200) {
						toaster.pop('success', '成功', '删除信息成功。');
						$scope.query($scope.bigCurrentPage);
					} else {
						toaster.pop('error', '错误', '删除信息失败！');
					}
				}, function () { toaster.pop('error', '错误', '删除信息失败！'); });
			}
		});
	};

	$scope.exportExcel = function () {
		if ($scope.statusList.selected != undefined) {
			$scope.queryMap.status = $scope.statusList.selected.id;
		} else if ($scope.statusList.selected == undefined) {
			delete $scope.queryMap.status;
		}
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			$scope.queryMap.createTime = starTime + "#@" + endTime;
		} else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
			delete $scope.queryMap.createTime;
		}
		ajax.call(new Req.POST({
			fields: $scope.queryMap,
			resource: 'order/rt/export'
		})).then(function (result) {
			if (result.code == 200) {
				window.open(result.result);
				toaster.pop('success', '成功', '导出信息成功。');
			} else {
				toaster.pop('error', '错误', '导出信息失败！！');
			}
		}, function () { toaster.pop('error', '错误', '导出信息失败！'); });
	};

	$scope.total = function () {
		if ($scope.statusList.selected != undefined) {
			$scope.queryMap.status = $scope.statusList.selected.id;
		} else if ($scope.statusList.selected == undefined) {
			delete $scope.queryMap.status;
		}
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			$scope.queryMap.createTime = starTime + "#@" + endTime;
		} else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
			delete $scope.queryMap.createTime;
		}
		ajax.call(new Req.GET({
			params: $scope.queryMap,
			resource: 'order/2/summary'
		})).then(function (result) {
			$scope.summary = result.result;
			$scope.totalCount = $scope.summary[0];
			$scope.orderTotal = $scope.summary[1]; //订单总额
			$scope.orderPaymentTotal = $scope.summary[2]; //订单支付总金额
			$scope.orderRefundToatal = $scope.summary[3]; //退款金额
			$scope.insurancesToatal = $scope.summary[4]; //保险金额
			$scope.tickets = $scope.summary[5]; //票数
			$scope.flag = true;
		}, function () { });
	};
});
